﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using Jungle.Code.Utils;
using Jungle.Code.Core;
using System.Xml;

namespace Jungle.Articles.WebControls
{
    public class AList : WebControl
    {
        private Language lang;
        private string[] langParams;
        // -------------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            LoadLanguage();
            Article news = new Article();
            news.LanguageId = lang.LangID;
            news.GetAllInList();

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write("<strong>"+langParams[0]+"</strong>");
            writer.RenderEndTag();
            int c = 0;
            foreach (Article n in news.NewsLisr)
            {
                writer.AddAttribute("class", "div_VNList_el");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.Write("<strong>" + n.PubDate.ToShortDateString() + " - " + n.Title + "</strong><br/>");
                writer.Write(CreatePreText(n.Description, 100));
                writer.Write("...");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/" + lang.Name + "/article.aspx?i=" + n.NewId.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(langParams[1]);
                writer.RenderEndTag();
                writer.RenderEndTag();
                if (c == 1) break;
                c++;
            }
            base.RenderContents(writer);
        }
        // -------------------------------------------------------------------------------
        private string CreatePreText(string input, int lenght)
        {
            input = Regex.Replace(input, "<[^>]*?>", string.Empty, RegexOptions.IgnoreCase);
            if(input.Length > lenght)
                input = input.Substring(0, lenght);
            if(input.IndexOf(" ") != -1)
                input = input.Substring(0, input.LastIndexOf(" "));

            return input;
        }
        // -------------------------------------------------------------------------------
        private HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        // -------------------------------------------------------------------------------
        private void LoadLanguage()
        {
            string ControlPath = "/Modules/Articles";
            int LangId = Node.GetByUrl(UrlHelper.GetRequestUrl().Substring(0, UrlHelper.GetRequestUrl().IndexOf(".aspx")).Replace("/main", "")).LangID;
            Language l = new Language(LangId, "", "", true, 0); l.GetById();
            lang = l;

            XmlDocument langDoc = new XmlDocument();
            langDoc.Load(Context.Server.MapPath(ControlPath + "/langs/" + l.Name + ".xml"));
            langParams = new string[langDoc.ChildNodes[1].ChildNodes.Count];
            for (int i = 0; i < langDoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                langParams[i] = langDoc.ChildNodes[1].ChildNodes[i].InnerText;
            }
        }
    }
}
